package com.example.mybugshaker.flow.email;

import io.reactivex.Scheduler;
import io.reactivex.disposables.Disposable;
import io.reactivex.disposables.Disposables;
import io.reactivex.plugins.RxJavaPlugins;
import ohos.eventhandler.EventHandler;

import java.util.concurrent.TimeUnit;

public class HandlerScheduler extends Scheduler {
    private final EventHandler handler;
    HandlerScheduler(EventHandler ehandler) {
        this.handler = ehandler;
    }
    @Override
    public final Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit) {
        if (run == null) throw new NullPointerException("run == null");
        if (unit == null) throw new NullPointerException("unit == null");
        Runnable runs;
        runs = RxJavaPlugins.onSchedule(run);
        ScheduledRunnable scheduled = new ScheduledRunnable(handler, runs);
        handler.postTask(scheduled, unit.toMillis(delay));
        return scheduled;
    }
    @Override
    public final Worker createWorker() {
        return new HandlerWorker(handler);
    }
    private static final class HandlerWorker extends Worker {
        private final EventHandler handler;
        private volatile boolean disposed;
        HandlerWorker(EventHandler ehandler) {
            this.handler = ehandler;
        }
        @Override
        public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
            if (run == null) throw new NullPointerException("run == null");
            if (unit == null) throw new NullPointerException("unit == null");
            if (disposed) {
                return Disposables.disposed();
            }
            Runnable runs;
            runs = RxJavaPlugins.onSchedule(run);
            ScheduledRunnable scheduled = new ScheduledRunnable(handler, runs);
            handler.postTask(scheduled, unit.toMillis(delay));
            if (disposed) {
                handler.removeAllEvent();
                return Disposables.disposed();
            }
            return scheduled;
        }
        @Override
        public void dispose() {
            disposed = true;
            handler.removeAllEvent();
        }
        @Override
        public boolean isDisposed() {
            return disposed;
        }
    }
    private static final class ScheduledRunnable implements Runnable, Disposable {
        private final EventHandler handler;
        private final Runnable delegate;
        private volatile boolean disposed;
        ScheduledRunnable(EventHandler ehandler, Runnable rdelegate) {
            this.handler = ehandler;
            this.delegate = rdelegate;
        }
        @Override
        public void run() {
            try {
                delegate.run();
            } catch (Exception t) {
                RxJavaPlugins.onError(t);
            }
        }
        @Override
        public void dispose() {
            disposed = true;
            handler.removeAllEvent();
        }
        @Override
        public boolean isDisposed() {
            return disposed;
        }
    }

}
